﻿using System;

namespace CharacterCreation.Business.Calculations {
    /// <summary>
    /// 1 = 3
    /// 1 = 2
    /// 1 = 1
    /// 3 = 2
    /// 2 = 1
    /// </summary>
    public static class LevelCalculations {
        public static int CalculateSkill(int statistic, int xp) {
            decimal total = statistic;
            while (xp > 0) {
                if (total < statistic * 2) {
                    total = total + 3;
                    xp = xp - 1;
                }
                else if (total < statistic * 3) {
                    total = total + 2;
                    xp = xp - 1;
                }
                else if (total < statistic * 4) {
                    total = total + 1;
                    xp = xp - 1;
                }
                else if (total < statistic * 5) {
                    total = total + (2 / 3);
                    xp = xp - 1;
                }
                else if (total < statistic * 6) {
                    total = total + (1 / 2);
                    xp = xp - 1;
                }
                else if (total < statistic * 7) {
                    total = total + (1 / 3);
                    xp = xp - 1;
                }
                else if (total < statistic * 8) {
                    total = total + (1 / 4);
                    xp = xp - 1;
                }
                else {
                    total = total + (1 / 5);
                    xp = xp - 1;
                }
            }
            return (int)Math.Round(total, 0);
        }

        /// <summary>
        /// 1 = 2
        /// 1 = 1
        /// 3 = 2
        /// 2 = 1
        /// 3 = 1
        /// </summary>
        /// <param name="statistic"></param>
        /// <param name="xp"></param>
        /// <returns></returns>
        public static int CalculateSpecial(int statistic, int xp) {
            decimal total = statistic;
            while (xp > 0) {
                if (total < statistic * 2) {
                    total = total + 2;
                    xp = xp - 1;
                }
                else if (total < statistic * 3) {
                    total = total + 1;
                    xp = xp - 1;
                }
                else if (total < statistic * 4) {
                    total = total + (2 / 3);
                    xp = xp - 1;
                }
                else if (total < statistic * 5) {
                    total = total + (1 / 2);
                    xp = xp - 1;
                }
                else if (total < statistic * 6) {
                    total = total + (1 / 3);
                    xp = xp - 1;
                }
                else if (total < statistic * 7) {
                    total = total + (1 / 4);
                    xp = xp - 1;
                }
                else {
                    total = total + (1 / 5);
                    xp = xp - 1;
                }
            }
            return (int)Math.Round(total, 0);
        }
        
        /// <summary>
        /// 2 = 5
        /// 2 = 3
        /// 3 = 4
        /// </summary>
        /// <param name="statistic"></param>
        /// <param name="xp"></param>
        /// <returns></returns>
        public static int CalculateEnergy(int statistic, int xp) {
            decimal total = statistic;
            while (xp > 0) {
                if (total < statistic * 2) {
                    total = total + 2.5M;
                    xp = xp - 1;
                }
                else if (total < statistic * 3) {
                    total = total + (3 / 2);
                    xp = xp - 1;
                }
                else if (total < statistic * 4) {
                    total = total + (4 / 3);
                    xp = xp - 1;
                }
                else if (total < statistic * 5) {
                    total = total + 1;
                    xp = xp - 1;
                }
                else if (total < statistic * 6) {
                    total = total + (3 / 4);
                    xp = xp - 1;
                }
                else {
                    total = total + (1 / 2);
                    xp = xp - 1;
                }
            }
            return (int)Math.Round(total, 0);
        }


        public static int CalculateStatistic(int statistic, int xp) {
            decimal total = statistic;
            while (xp > 0) {
                if (total < 24) {
                    total = total + (1M / 3M);
                    xp = xp - 1;
                }
                else if (total < 30) {
                    total = total + (1M / 4M);
                    xp = xp - 1;
                }
                else if (total < 36) {
                    total = total + (1M / 5M);
                    xp = xp - 1;
                }
                else if (total < 42) {
                    total = total + (1M / 6M);
                    xp = xp - 1;
                }
                else {
                    total = total + (1M / 7M);
                    xp = xp - 1;
                }
            }
            return (int)Math.Round(total, 0);
        }


        public static int CalculateSkillXPCost(int statistic, int targetLevel) {
            var xp = 0;
            decimal total = targetLevel;

            while (total > statistic) {
                if (total < statistic * 2) {
                    total = total - 3;
                    xp++;
                }
                else if (total < statistic * 3) {
                    total = total - 2;
                    xp++;
                }
                else if (total < statistic * 4) {
                    total = total - 1;
                    xp++;
                }
                else if (total < statistic * 5) {
                    total = total - (2M / 3M);
                    xp++;
                }
                else if (total < statistic * 6) {
                    total = total - (1M / 2M);
                    xp++;
                }
                else if (total < statistic * 7) {
                    total = total - (1M / 3M);
                    xp++;
                }
                else if (total < statistic * 8) {
                    total = total - (1M / 4M);
                    xp++;
                }
                else {
                    total = total - (1M / 5M);
                    xp++;
                }
            }

            return xp - 1;
        }
    }
}
